#include "widgetcan.h"
#include "ui_widgetcan.h"
#include "canbusiothread.h"
#include <QMessageBox>
#include <QIntValidator>



WidgetCAN::WidgetCAN(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetCAN)
{
    ui->setupUi(this);

    QValidator *validator=new QIntValidator(0,10000,this);
    ui->etCANTxId->setValidator(validator);
    ui->etCANRxId->setValidator(validator);

}

WidgetCAN::~WidgetCAN()
{
    delete ui;
}

void WidgetCAN::SetCanbusIo(CanBusIoThread *tCanBusIoThread, QString productModal)
{
     this->mCanbusThread = tCanBusIoThread;
     this->mProductModal = productModal;

	 if (this->mProductModal == "TF03")
	 {
		 //ui->cbOutputFormat->clear();
		 //ui->cbOutputFormat->addItem("9Byte(cm)");
		 //ui->cbOutputFormat->addItem("字符格式(m)");

	 }

}

uint WidgetCAN::GetCanTxId()
{
	return ui->etCANTxId->text().toUInt();
}



bool WidgetCAN::CheckComState()
{
    if(mCanbusThread==nullptr)
        return false;

    if(!mCanbusThread->isOpen())
    {
        QMessageBox::warning(NULL, "Error", tr(u8"未连接CAN"));
        return false;
    }

    return true;
}



void WidgetCAN::on_btnFrameFreq_clicked()
{
    if(!CheckComState()) return;
	qDebug() << "帧率设定";

    if(mCanbusThread->canSetFrameFreq(ui->cbFrameFreq->currentText().toUShort()))
    {
       qDebug("帧率设定成功！");
       QMessageBox::information(NULL, tr(u8"提示"), tr(u8"帧率设定成功"));

    }else{
        qDebug("帧率设定失败！");
        QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"帧率设定失败"));
    }
//    bool ret = canbusThread->OpenCAN();

//    if(ret){
//        canbusThread->start();
//    }
}

void WidgetCAN::on_btnOutSwitch_clicked()
{
    if(!CheckComState()) return;

   int index = ui->cbOutputSwitch->currentIndex();
   bool switchOn;
   if(index==0){
       switchOn=true;
	   qDebug() << "开启输出";
   }else {
       switchOn=false;
	   qDebug() << "关闭输出";
    }

    if(mCanbusThread->canOutputSwitch(switchOn))
    {
       qDebug("设定成功！");
       QMessageBox::information(NULL, tr(u8"提示"), tr(u8"输出启/禁用成功！"));
    }
    else
    {
       qDebug("设定失败");
       QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"输出启/禁用失败！"));
    }

}



void WidgetCAN::on_btnVersion_clicked()
{
    if(!CheckComState()) return;
     bool result;
	 qDebug() << "读取版本号";
     QString  s_version= mCanbusThread->canGetVersion(result);
     if(false == result)
     {
          ui->lbVersion->setText(tr(u8"版本号获取失败"));
		  qDebug() << "读取的版本号失败";
     }else{
          ui->lbVersion->setText(s_version);
		  qDebug() << "读取的版本号："<<s_version;
     }


}

void WidgetCAN::on_btnSaveConfig_clicked()
{
    if(!CheckComState()) return;

	qDebug() << "保存配置";
    ui->lbConfigResult->setText("");
    if(mCanbusThread->canSaveConfig())
    {
        ui->lbConfigResult->setText(tr(u8"配置保存成功！"));
		qDebug() << "配置保存成功";
    }else{
       ui->lbConfigResult->setText(tr(u8"配置保存失败！"));
	   qDebug() << "配置保存失败";
    }

}

void WidgetCAN::on_btnSystemReset_clicked()
{
    if(!CheckComState()) return;
	qDebug() << "复位雷达";
    QMessageBox  msgBox;
   if(mCanbusThread->canSystemReset())
   {
      qDebug("复位成功！");
      //QMessageBox::information(NULL, tr(u8"提示"), tr(u8"复位成功！"));
      ui->lbResetResult->setText(tr(u8"复位成功！"));


   }else{
      qDebug("复位失败");
      //QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"复位失败"));
       ui->lbResetResult->setText(tr(u8"复位失败！"));
   }
}

void WidgetCAN::on_btnFactoryReset_clicked()
{
     if(!CheckComState()) return;
	 qDebug() << "恢复出厂设置";
     if(mCanbusThread->canFactoryReset())
     {
         ui->lbFactoryResetResult->setText(tr(u8"恢复出厂成功"));
		 qDebug() << "恢复出厂成功";
     }else{
         ui->lbFactoryResetResult->setText(tr(u8"恢复出厂失败"));
		 qDebug() << "恢复出厂失败";
     }
}



void WidgetCAN::on_btnCANParamSet_clicked()
{
    uint frameType = ui->cbFrameTyp->currentIndex();
    uint baudrate = ui->cbBaudrate->currentText().toInt();

    if(ui->etCANTxId->text()==""){

        QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"CAN发送ID是必填项"));
        return;
    }

    if(ui->etCANRxId->text()==""){

        QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"CAN接收ID是必填项"));
        return;
    }

    ulong txId = ui->etCANTxId->text().toULong();
    ulong rxId = ui->etCANRxId->text().toULong();

    if(!CheckComState()) return;

	qDebug() << "设定CAN参数";

    if(mProductModal=="TF03")
    {
		bool bResult = false;
		qDebug() << "设定CAN参数[帧类型]:"<<ui->cbFrameTyp->currentText();
		bResult = mCanbusThread->canSetFrameType(frameType);

		if (!bResult) {
			qDebug() << "设定CAN参数失败[帧类型]:" << ui->cbFrameTyp->currentText();
			QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"CAN参数设置失败"));
			return;
		}
		qDebug() << "设定CAN参数成功[帧类型]:" << ui->cbFrameTyp->currentText();


		qDebug() << "设定CAN参数[波特率]:" << ui->cbBaudrate->currentText();
		bResult = mCanbusThread->canBaudrate(baudrate);
		
		if (!bResult) {
			qDebug() << "设定CAN参数失败[波特率]:" << ui->cbBaudrate->currentText();
			QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"CAN参数设置失败"));
			return;
		}



		qDebug() << "设定CAN参数[发送ID]:" << txId;
		bResult = mCanbusThread->canSetTxID(txId);
		
		if (!bResult) {
			qDebug() << "设定CAN参数失败[发送ID]:" << txId;
			QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"CAN参数设置失败"));
			return;
		}
		qDebug() << "设定CAN参数成功[发送ID]:" << txId;


		qDebug() << "设定CAN参数[接收ID]:" << rxId;
		bResult = mCanbusThread->canSetRxID(rxId);
		if (!bResult) {
			qDebug() << "设定CAN参数失败[接收ID]:" << rxId;
			QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"CAN参数设置失败"));
			return;
		}

		else
		{
			qDebug() << "设定CAN参数成功[接收ID]:" << rxId;
			QMessageBox::information(NULL, tr(u8"提示"), tr(u8"CAN参数设置成功"));
		}
    }
    else 
	{
		qDebug() << "设定CAN参数[帧类型]:" << ui->cbFrameTyp->currentText()
			<< "[波特率]" << baudrate << "[发送ID]" << txId << "[接收ID]" << rxId;

		bool bResult = mCanbusThread->canSetParam(frameType,baudrate,rxId,txId);

		if (!bResult) {
			qDebug() << "CAN参数设置失败";
			QMessageBox::warning(NULL, tr(u8"提示"), tr(u8"CAN参数设置失败"));
			return;
		}

		else
		{
			qDebug() << "CAN参数设置成功";
			QMessageBox::information(NULL, tr(u8"提示"), tr(u8"CAN参数设置成功"));
		}
    }
}
